package com.tcd.policy.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

public class AutoCode {
    public static void main(String[] args) {
        AutoGenerator mpg = new AutoGenerator();
        //配置策略
        //1. 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");//获取当前目录
        gc.setOutputDir(projectPath+"/cloud-CarInsuranceClaims-Policy/src/main/java");  //配置生成的路径
        gc.setAuthor("cz");  //设置作者
        gc.setOpen(false);  //是否打开资源管理器(文件夹)
        gc.setFileOverride(false);  //是否覆盖
        gc.setServiceName("%sService"); //去掉service前面的I前缀
        gc.setIdType(IdType.ASSIGN_ID); //配置id自动生成策略
        gc.setDateType(DateType.ONLY_DATE); //配置Date类型(只是时间)
        gc.setSwagger2(true);   //配置Swagger(前后端分离？)
        mpg.setGlobalConfig(gc);
        //2. 设置数据源
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUrl("jdbc:mysql://49.235.249.4:3306/carinsuranceclaims?userUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);
        //3. 包的配置
        PackageConfig pc = new PackageConfig();
        //pc.setModuleName("blog");
        pc.setParent("com.cz");
        pc.setEntity("pojo");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setController("controller");
        mpg.setPackageInfo(pc);
        //4. 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("risk_type_info");    //设置要映射的表名
        strategy.setNaming(NamingStrategy.underline_to_camel);  //包命名规则，下划线转驼峰
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);    //字段名下划线转驼峰
        strategy.setEntityLombokModel(true);    //自动lombok
        strategy.setRestControllerStyle(false);  //控制器是否支持 restful风格
        strategy.setLogicDeleteFieldName("deleted");    //设置逻辑删除的字段
        strategy.setVersionFieldName("version");    //乐观锁
        strategy.setControllerMappingHyphenStyle(true); //localhost:8080/hello_id_2

        //设置自动填充配置
        ArrayList<TableFill> talbeFills = new ArrayList<>();
        talbeFills.add(new TableFill("create_time", FieldFill.INSERT));
        talbeFills.add(new TableFill("update_time", FieldFill.INSERT_UPDATE));
        strategy.setTableFillList(talbeFills);

        mpg.setStrategy(strategy);
        //执行
        mpg.execute();
    }
}
